import vue from 'vue'
import Vuex from 'vuex'
import moduleProduct from './modules/products'

vue.use(Vuex)

const store = new Vuex.Store({
    state:{
        count:0,
        todos:[
            {id:1, title:'7777',done:true},
            {id:2, title:'7777',done:false},
            {id:3, title:'7777',done:true},
            {id:4, title:'7777',done:false},
        ],
        userInfo:{
            id:0,
            username:'小叶子',
            sex:'女',
            avatar:'',
            access:[]
        }
    },
    mutations:{
        increament:function(state){
            state.count ++
        },
        increamentBy:function(state,num){
            state.count += num
        },
        updateUsername:function(state,newUsername){
            state.username = newUsername
        }
    },
    getters:{
        todeDone:state=>{
            return state.todos.filter(item => item.done === true)
        }
    },
    actions:{
        getUserInfo:function({commit},token){
             //     context.state.username = token
            setTimeout(()=>{
                console.log('2秒后运行')
            },2000)
            commit('updateUsername',token)
        },        
    },
    modules:{
        product:moduleProduct
    }
})
console.log(store.state.count)

store.commit('increament',20)

store.commit('updateUsername','index.js中的newname')

export default store

